import { message } from 'antd';

import {
  queryOrganProfile,
  submitOrganProfile,
} from '../services/api';

export default {
  namespace: 'organProfile',

  state: {
    loading: false,
  },

  effects: {
    * fetchBasic({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryOrganProfile, payload);
      yield put({
        type: 'loadProfile',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        loading: false,
      });
    },

    * submitProfile({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(submitOrganProfile, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (response.code === 'ok') {
        message.info('更新个人资料成功');
      }
    },
  },

  reducers: {
    loadProfile(state, { payload }) {
      return {
        ...state,
        form: payload,
        status: 'ok',
      };
    },

    changeSubmitting(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },
  },
};
